var slides = new Array
var oldSlides = new Array
var curSlideIdx = 0

function arraysIdentical(a, b) {
    var i = a.length;
    if (i != b.length) return false;
    while (i--) {
        if (a[i] !== b[i]) return false;
    }
    return true;
};

function run()
{
    var folders = tools.ls(imagesFolderPath, true, false)
    slides = []
    var slidesToPush = new Array
    for (var i = 0; i < folders.length; ++i) {
//        console.log(folders[i])
        if (excludeFolders.indexOf(i) !== -1) {
//            console.log("excluded")
            continue
        }
        slidesToPush = tools.ls(folders[i], false, false)
        for (var j = 0; j < slidesToPush.length; ++j)
            slides.push(slidesToPush[j])
    }

    if (!arraysIdentical(slides, oldSlides)) {
        curSlideIdx = 0;
        oldSlides = slides
        console.log("RUN AGAIN")
    }
//    for (i = 0; i < slides.length; ++i)
//        console.log("img", slides[i]);

    if (slides.length <= 0) {
        showEmptyWarning()
        return;
    }
    slide.source = "image://threaded/" + slides[curSlideIdx];
    fetchNext()
    slide.opacity = 0.0
    animation.start()
}

function stop()
{
    animation.stop()
}

function slideShowed()
{
    if (!slideshow.running)
        return;
    if (++curSlideIdx >= slides.length)
        curSlideIdx = 0;
    slide.source = "image://threaded/" + slides[curSlideIdx];
    fetchNext();
    animation.start()
}

function fetchNext()
{
    var nextIdx = curSlideIdx + 1;
    if (nextIdx >= slides.length)
        nextIdx = 0;
    threaded.fetch(slides[nextIdx]);
}

function showEmptyWarning()
{
    console.log("empty")
}
